package com.lib76.socket.impl;

import java.nio.charset.Charset;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.bootstrap.ConnectionlessBootstrap;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder;
import org.jboss.netty.handler.codec.frame.Delimiters;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;

import com.lib76.socket.SocketObject;
import com.lib76.socket.model.SocketInfo;

/**
 * 通过TCP协议使用字符串报文通讯的Socket对象
 * @author 张浩
 * @version 1.0.0
 */
public class StringTcpSocketObject extends SocketObject{

	public StringTcpSocketObject(SocketInfo info) {
		super(info);
	}

	@Override
	protected Channel runServiceSocket() throws Exception {
		ChannelFactory factory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
	    ServerBootstrap bootstrap = new ServerBootstrap(factory);
	    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() {
                ChannelPipeline pipeline = Channels.pipeline();
                pipeline.addLast("framer", new DelimiterBasedFrameDecoder(1024*100, Delimiters.lineDelimiter()));
                pipeline.addLast("decoder", new StringDecoder(Charset.forName("UTF-8")));
                pipeline.addLast("encoder", new StringEncoder(Charset.forName("UTF-8")));
                pipeline.addLast("handler",new Handler());
                return pipeline;
            }
        });

        bootstrap.setOption("child.tcpNoDelay", true);
        bootstrap.setOption("child.keepAlive", true);

        Channel channel = bootstrap.bind(info.getLocalSocketAddress());
        return channel;
	}

	@Override
	protected ClientBootstrap runTcpClientSocket() throws Exception {
		ChannelFactory factory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
		ClientBootstrap bootstrap = new ClientBootstrap(factory);
	    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() {
                ChannelPipeline pipeline = Channels.pipeline();
                pipeline.addLast("framer", new DelimiterBasedFrameDecoder(1024*100, Delimiters.lineDelimiter()));
                pipeline.addLast("decoder", new StringDecoder(Charset.forName("UTF-8")));
                pipeline.addLast("encoder", new StringEncoder(Charset.forName("UTF-8")));
                pipeline.addLast("handler",new Handler());
                return pipeline;
            }
        });

        bootstrap.setOption("child.tcpNoDelay", true);
        bootstrap.setOption("child.keepAlive", true);
        
        return bootstrap;
	}

	@Override
	protected ConnectionlessBootstrap runUdpClientSocket() throws Exception {
		return null;
	}

}
